package com.ywyself.controller.admin;

import com.jfinal.core.ActionKey;
import com.ywyself.common.pool.DataLoad;
import com.ywyself.controller.base.YsController;
import com.ywyself.ext.kit.YsKit;
import com.ywyself.ext.utils.YsStringUtils;
import com.ywyself.model.Option;
import com.ywyself.service.OptionService;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统设置
 *
 * @author YuWenYue
 * @date 2017-10-10 20:00:43
 *
 */
public class SettingController extends YsController {

	/**
	 * 系统设置
	 */
	@ActionKey(ADMIN_ROOT + "/setting")
	public void index() {
		forwardAction(ADMIN_ROOT + "/setting/base");
	}

	/**
	 * 系统设置 - 站点设置
	 */
	@ActionKey(ADMIN_ROOT + "/setting/base")
	public void base() {
		setPageTitle("站点设置");
		setAttr("tab", "setting_base");
		render(BACK_STAGE + "/setting/base.html");
	}

	/**
	 * 系统设置 - 站点设置 - 保存
	 */
	@ActionKey(ADMIN_ROOT + "/setting/base/save")
	public void baseSave() {
		// 站点名称
		String siteName = getPara("site_name") == null ? "" : getPara("site_name");
		// 站点logo
		String siteLogo = getPara("site_logo") == null ? "" : getPara("site_logo");
		// 站点根网址
		String siteRoot = getPara("site_root") == null ? "" : getPara("site_root");
		// 站点备案号
		String ICP = getPara("icp") == null ? "" : getPara("icp");
		// 站点版权信息
		String copyright = getPara("copyright") == null ? "" : getPara("copyright");
		// 网站状态open/close
		String siteStatus = getPara("site_status") == null ? "" : getPara("site_status");
		// 站点关闭的原因
		String close_reason = getPara("close_reason") == null ? "" : getPara("close_reason");
		switch (siteStatus) {
		case "open":
			siteStatus = Option.STATUS_OPEN;
			break;
		default:
			siteStatus = Option.STATUS_CLOSE;
			break;
		}
		Map<String, String> mapKey = new HashMap<String, String>(5);
		mapKey.put("siteName", siteName);
		mapKey.put("siteLogo", siteLogo);
		mapKey.put("siteRoot", siteRoot);
		mapKey.put("ICP", ICP);
		mapKey.put("copyright", copyright);
		mapKey.put("siteStatus", siteStatus);
		if (YsStringUtils.isEqual(siteStatus, Option.STATUS_CLOSE)) {
			mapKey.put("closeReason", close_reason);
		}
		YsKit retUpdate = OptionService.EXC.update(mapKey);
		if (retUpdate.isOk()) {
			DataLoad.loadSiteConfig(); // 重新加载数据
			YsKit.ok().code("101").msg("网站信息更新成功").render(this);
		} else {
			retUpdate.render(this);
		}
	}

	/**
	 * 系统设置 - 站长信息
	 */
	@ActionKey(ADMIN_ROOT + "/setting/info")
	public void info() {
		setPageTitle("站长信息");
		setAttr("tab", "setting_info");
		render(BACK_STAGE + "/setting/info.html");
	}

	/**
	 * 系统设置 - 站长信息
	 */
	@ActionKey(ADMIN_ROOT + "/setting/info/save")
	public void infoSave() {
		String userName = getPara("user_name"); // 站长姓名
		String userMotto = getPara("user_motto"); // 站长座右铭
		String userContact = getPara("user_contact"); // 站长Email
		Map<String, String> mapKey = new HashMap<String, String>(5);
		mapKey.put("userName", userName);
		mapKey.put("userMotto", userMotto);
		mapKey.put("userContact", userContact);

		YsKit retUpdate = OptionService.EXC.update(mapKey);
		if (retUpdate.isOk()) {
			DataLoad.loadSiteConfig(); // 重新加载数据
			YsKit.ok().code("101").msg("网站信息更新成功").render(this);
		} else {
			retUpdate.render(this);
		}
	}

	/**
	 * 系统设置 - SEO设置
	 */
	@ActionKey(ADMIN_ROOT + "/setting/seo")
	public void seo() {
		setPageTitle("SEO设置");
		setAttr("tab", "setting_seo");
		render(BACK_STAGE + "/setting/seo.html");
	}

	/**
	 * 系统设置 - SEO设置 - 保存
	 */
	@ActionKey(ADMIN_ROOT + "/setting/seo/save")
	public void seoSave() {
		String titleAdd = getPara("title_add"); // 标题附加字
		String keywords = getPara("keywords"); // 网站关键词
		String description = getPara("description"); // 关键词描述
		Map<String, String> mapKey = new HashMap<String, String>(5);
		mapKey.put("titleAdd", titleAdd);
		mapKey.put("keywords", keywords);
		mapKey.put("description", description);

		YsKit retUpdate = OptionService.EXC.update(mapKey);
		if (retUpdate.isOk()) {
			DataLoad.loadSiteConfig(); // 重新加载数据
			YsKit.ok().code("101").msg("网站信息更新成功").render(this);
		} else {
			retUpdate.render(this);
		}
	}

	/**
	 * 系统设置 - 其他设置
	 */
	@ActionKey(ADMIN_ROOT + "/setting/other")
	public void other() {
		setPageTitle("其他设置");
		setAttr("tab", "setting_other");
		render(BACK_STAGE + "/setting/other.html");
	}

	/**
	 * 系统设置 - 其他设置 - 保存
	 */
	@ActionKey(ADMIN_ROOT + "/setting/other/save")
	public void otherSave() {
		// 每页数据条数
		String pageSize = getPara("page_size");
		// 头部代码
		String headCode = getPara("head_code");
		// 底部代码
		String footCode = getPara("foot_code");
		Map<String, String> mapKey = new HashMap<String, String>(5);
		mapKey.put("pageSize", pageSize);
		mapKey.put("headCode", headCode);
		mapKey.put("footCode", footCode);

		YsKit retUpdate = OptionService.EXC.update(mapKey);
		if (retUpdate.isOk()) {
			DataLoad.loadSiteConfig(); // 重新加载数据
			YsKit.ok().code("101").msg("网站信息更新成功").render(this);
		} else {
			retUpdate.render(this);
		}
	}

	/**
	 * 系统设置 - 数据库字段补充，扩展
	 */
	@ActionKey(ADMIN_ROOT + "/setting/added")
	public void added() {
		setPageTitle("扩展字段");
		setAttr("tab", "setting_added");
		render(BACK_STAGE + "/setting/added.html");
	}

	/**
	 * 系统设置 - 数据库字段补充，扩展 - 保存
	 */
	@ActionKey(ADMIN_ROOT + "/setting/added/save")
	public void addedSave() {
		// 数据的键
		String cfgKey = getPara("cfg_key");
		// 数据的值
		String cfgVal = getPara("cfg_val");
		// 数据用途,备注
		String cfgDesc = getPara("cfg_desc");
		YsKit.ok().msg(cfgKey + ":" + cfgVal + "," + cfgDesc).render(this);
	}
}
